<script setup>
import {ref} from "vue";
import { useRouter } from 'vue-router'
// import {ElMessage} from "element-plus";

const data = ref({
  username: '',
  password: '',
  user_type:'',
  email:'',
  confirmPassword:'',
  phone:''
})
const loading = ref(false);
const handleRegister = async () => {
  // 1. 前端验证
    if (!data.value.user_type) {
    ElMessage.error('请选择用户类型')
    return
  }

  if (!data.value.username || !data.value.password || !data.value.email) {
    ElMessage.error('请填写完整信息')
    return
  }
  if (data.value.password !== data.value.confirmPassword) {
    ElMessage.error('两次输入的密码不一致')
    return
  }
  if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(data.value.email)) {
    return ElMessage.warning('请输入有效的邮箱地址')
  }

  try {
    loading.value = true

    // 2. 直接提交整个 data.value（会自动解构 ref）
    const response = await fetch('http://127.0.0.1:8000/api/register', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        ...data.value,
        confirmPassword: undefined // 移除不需要的字段
      })
    })

    const result = await response.json()

    if (response.ok) {
      ElMessage.success('注册成功')
      router.push('/login')
    } else {
      ElMessage.error(result.detail || '注册失败')
    }
  } catch (error) {
    // console.error(error)  // 这里可以查看具体的错误信息
    ElMessage.error('网络错误，请重试')
  } finally {
    loading.value = false
  }
}

const statusOptions = [
  { value: '在职-考虑机会', label: '在职-考虑机会' },
  { value: '离职-随时到岗', label: '离职-随时到岗' },
  { value: '在校学生', label: '在校学生' }
]
const router = useRouter()
</script>

<template>
  <div class="register-container">
    <div class="register-background"></div>
    <div class="register-content">
      <el-card class="register-card">
        <template #header>
          <div class="register-header">
            <div class="logo-container">
              <img src="../assets/images/logo.png" alt="WIN-WIN招聘平台" class="logo-image"/>
            </div>
            <h2>用户注册</h2>
            <p>请选择您的身份类型并填写相关信息</p>
          </div>
        </template>

        <el-form
          :model="data"
          label-width="100px"
          class="register-form"
        >
          <el-form-item label="用户类型" prop="user_type">
            <el-radio-group v-model="data.user_type" class="user-type-group">
              <el-radio-button label="job_seeker">求职者</el-radio-button>
              <el-radio-button label="recruiter">招聘者</el-radio-button>
            </el-radio-group>
          </el-form-item>

          <el-form-item label="用户名">
            <el-input 
              v-model="data.username" 
              placeholder="请输入用户名"
              class="custom-input"
            />
          </el-form-item>
          
          <el-form-item label="邮箱" prop="email">
            <el-input 
              v-model="data.email" 
              placeholder="请输入邮箱"
              class="custom-input"
            />
          </el-form-item>

          <el-form-item label="密码" prop="password">
            <el-input
              v-model="data.password"
              type="password"
              placeholder="请输入密码"
              show-password
              class="custom-input"
            />
          </el-form-item>
          
          <el-form-item label="确认密码" prop="confirmPassword">
            <el-input
              v-model="data.confirmPassword"
              type="password"
              placeholder="请再次输入密码"
              show-password
              class="custom-input"
            />
          </el-form-item>

          <template v-if="data.user_type === 'job_seeker'">
            <el-form-item label="联系电话" prop="phone">
              <el-input 
                v-model="data.phone"
                class="custom-input"
              />
            </el-form-item>
          </template>

          <template v-else>
            <el-form-item label="联系电话" prop="phone">
              <el-input 
                v-model="data.phone"
                class="custom-input"
              />
            </el-form-item>
          </template>
          
          <el-form-item class="form-buttons">
            <el-button
              type="primary"
              @click="handleRegister"
              :loading="loading"
              class="register-btn"
            >
              立即注册
            </el-button>
            <el-button 
              @click="router.push('/login')"
              class="login-btn"
            >
              已有账号，去登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  position: relative;
  overflow: hidden;
  padding: 40px 20px;
  background-color: transparent;
}

.register-background {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;

  filter: brightness(0.7);
  z-index: -2;
}

.register-content {
  width: 100%;
  max-width: 1200px;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  z-index: 1;
}

.register-card {
  width: 600px;
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  background-color: rgba(255, 255, 255, 0.4);
  backdrop-filter: blur(10px);
  animation: slideIn 0.5s ease-out;
  position: relative;
  z-index: 1;
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateX(-20px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

.logo-container {
  display: flex;
  justify-content: center;
  margin-bottom: 20px;
}

.logo-image {
  width: 120px;
  height: auto;
  transition: transform 0.3s;
}

.logo-image:hover {
  transform: scale(1.05);
}

.register-header {
  text-align: center;
  padding: 20px 0;
}

.register-header h2 {
  color: #303133;
  margin-bottom: 10px;
  font-weight: 600;
  font-size: 24px;
  background: linear-gradient(45deg, #409EFF, #36cfcf);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.register-header p {
  color: #909399;
  font-size: 14px;
}

.register-form {
  padding: 20px;
}

.user-type-group :deep(.el-radio-button__inner) {
  padding: 12px 20px;
  border-radius: 8px;
  transition: all 0.3s;
  
  &:hover {
    transform: translateY(-2px);
  }
}

.custom-input :deep(.el-input__wrapper) {
  border-radius: 8px;
  padding: 8px 15px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
  transition: all 0.3s;
  
  &:hover, &:focus-within {
    box-shadow: 0 2px 12px 0 rgba(64, 158, 255, 0.2);
  }
}

.form-buttons {
  margin-top: 30px;
  display: flex;
  justify-content: center;
  gap: 20px;
}

.register-btn {
  width: 200px;
  height: 45px;
  font-size: 16px;
  border-radius: 8px;
  background: linear-gradient(45deg, #409EFF, #36cfcf);
  border: none;
  transition: all 0.3s;
  
  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
  }
}

.login-btn {
  width: 200px;
  height: 45px;
  font-size: 16px;
  border-radius: 8px;
  transition: all 0.3s;
  
  &:hover {
    transform: translateY(-2px);
    color: #409EFF;
    border-color: #409EFF;
  }
}
</style>